package com.enzoism.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

// 针对【实现类】编写的Agent代理工具类
public class Agent implements InvocationHandler{

    Object target;

    public Agent(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("我是代理，谈合作，拉投资，布置场地");
        // 要调用的那个类[接口]的哪个方法，传入哪些值
        Object invoke = method.invoke(target, args);
        System.out.println("我是代理，项目结算");
        System.out.println();
        return invoke;
    }

    public static Object getProxy(Object target){
        Object instance = Proxy.newProxyInstance(MainClient.class.getClassLoader(), target.getClass().getInterfaces(), new Agent(target));
        return instance;
    }
}
